//
//  BaseTabBar.m
//  CharonTVProject
//
//  Created by karong on 2023/6/5.
//

#import "BaseTabBar.h"
@interface BaseTabBar ()
@end

@implementation BaseTabBar

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.translucent = YES;
        self.backgroundColor = UIColor.bg_tab_161A2B;
        [self setShadowImage:[[UIImage alloc]init]];
        [self setBackgroundImage:[[UIImage alloc]init]];
        _oldSafeAreaInsets = UIEdgeInsetsZero;
    }
    return self;
}

//更正item的位置
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if([keyPath isEqualToString:@"frame"]) {
        CGRect newFrame = [change[NSKeyValueChangeNewKey] CGRectValue];
        if (newFrame.size.height == 33) {
            newFrame.size.height = 48;
            ((UIView *)object).frame = newFrame;
        }
    }
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    NSArray *items = self.items;
    NSInteger index = 0;
    for (UITabBarItem *item in items) {
        // iOS13 push二级页面後返回 tabbar的文字會被重置成蓝色
        if (@available(iOS 13.0, *)) {
            self.tintColor = UIColor.font_yellow_FCB625;
            [self setUnselectedItemTintColor:UIColor.font_gray_484F5F];
        } else {
            NSDictionary *normalAttrDict = @{NSForegroundColorAttributeName:UIColor.font_gray_484F5F, NSFontAttributeName: [UIFont systemFontOfSize:11]};
            NSDictionary *selectedAttrDict = @{NSForegroundColorAttributeName:UIColor.font_yellow_FCB625, NSFontAttributeName: [UIFont systemFontOfSize:11]};
            [item setTitleTextAttributes:normalAttrDict forState:UIControlStateNormal];
            [item setTitleTextAttributes:selectedAttrDict forState:UIControlStateSelected];
        }
        item.imageInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        [item setTitlePositionAdjustment:UIOffsetMake(0, -2)];
        if (index == 0) {
            item.image = [WSIMG(@"tab_home_n") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            item.selectedImage = [WSIMG(@"tab_home_p") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            item.title = APPLocalized(@"广场");
        }
        else if (index == 1) {
            item.image = [WSIMG(@"tab_video_n") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            item.selectedImage = [WSIMG(@"tab_video_p") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            item.title = APPLocalized(@"看劇");
        }
        else if (index == 2) {
            item.image = [WSIMG(@"tab_welfare") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            item.selectedImage = [WSIMG(@"tab_welfare") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            item.title = APPLocalized(@"福利");
        }
        else {
            item.image = [WSIMG(@"tab_me_n") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            item.selectedImage = [WSIMG(@"tab_me_p") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            item.title = APPLocalized(@"我的" );
        }
        index ++;
    }
}

@end
